package com.pb.metadata.controller;

import com.pb.metadata.service.RunEnvironmentService;
import com.pb.infra.oth.entity.common.Page;
import com.pb.infra.oth.entity.common.Result;
import com.pb.infra.oth.util.Constant;
import com.pb.infra.oth.util.SysLog;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 运行环境管理
 * @author liaohaoyuan
 * @date 20210301
 */
@Slf4j
@RestController
@RequestMapping("v1/metadata/runEnvironment")
public class RunEnvironmentController {

    /**
     * 业务逻辑对象
     */
    @Resource
    private RunEnvironmentService runEnvironmentService;

    /**
     * 日志操作表
     */
    private static final String TABLE_NAME = "param_running_environment";

    /**
     * 日志操作功能名
     */
    private static final String OPERATION_MENU = "运行环境管理";

    /**
     * 查询分页数据
     *
     * @param map  查询参数
     * @param page 页码
     * @return Result 返回数据
     */
    @GetMapping("select")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.QUERY, menuName = OPERATION_MENU)
    public Result select(@RequestParam Map<String, Object> map, Page page) {
        return runEnvironmentService.select(map, page);
    }

    /**
     * 查询分页数据
     *
     * @param map  查询参数
     * @return Result 返回数据
     */
    @GetMapping("selectEnv")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.QUERY, menuName = OPERATION_MENU)
    public Result selectEnv(@RequestParam Map<String, Object> map) {
        return runEnvironmentService.selectEnv(map);
    }

    /**
     * 保存运行环境数据
     *
     * @param paramMap 请求参数
     * @return Result 返回数据
     */
    @PostMapping("insert")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.ADD, menuName = OPERATION_MENU)
    public Result insert(@RequestBody Map<String, Object> paramMap, HttpServletRequest request) {
        try {
            Result insert = runEnvironmentService.insert(paramMap, request);
            return insert;
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(Constant.EXIST, Constant.INSERT_ERROR_MSG);
        }
    }

    /**
     * 修改运行环境数据
     *
     * @param paramMap 请求参数
     * @return Result 返回数据
     */
    @PostMapping("update")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.MOD, menuName = OPERATION_MENU)
    public Result update(@RequestBody Map<String, Object> paramMap, HttpServletRequest request) {
        try {
            SimpleDateFormat format = new SimpleDateFormat("YYYY-MM-dd");
            paramMap.put("updateTime", format.format(new Date()));
            Result insert = runEnvironmentService.update(paramMap, request);
            return insert;
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(Constant.EXIST, Constant.INSERT_ERROR_MSG);
        }
    }


    /**
     * 删除运行环境数据
     *
     * @param paramMap 请求参数
     * @return Result 返回数据
     */
    @PostMapping("delete")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.DEL, menuName = OPERATION_MENU)
    public Result delete(@RequestBody List<String> paramMap) {
        runEnvironmentService.delete(paramMap);
        return Result.OK_EMPTY;
    }

    /**
     * 启用停用运行环境
     * @param paramMap 请求参数
     * @return Result 返回数据
     */
    @PostMapping("startOrStop")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.MOD, menuName = OPERATION_MENU)
    public Result startOrStop(@RequestBody List<Map<String,Object>> paramMap){
        runEnvironmentService.startOrStop(paramMap);
        return Result.OK_EMPTY;
    }

    /**
     * 运行环境管理参数配置保存
     *
     * @param paramMap 请求参数
     * @return Result 返回数据
     */
    @PostMapping("saveParam")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.ADD, menuName = OPERATION_MENU)
    public Result saveParam(@RequestBody Map<String, Object> paramMap) {
        try {
            Result insert = runEnvironmentService.saveParam(paramMap);
            return insert;
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(Constant.EXIST, Constant.INSERT_ERROR_MSG);
        }
    }

    /**
     * 测试数据库连接
     *
     * @param paramMap 请求参数
     * @return Result 返回数据
     */
    @PostMapping("testConnection")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.MOD, menuName = OPERATION_MENU)
    public Result testConnection(@RequestBody Map<String, Object> paramMap) {
        Map<String, Object> responseMap = new HashMap<>();
        responseMap = runEnvironmentService.connectionCheck(paramMap);
        return Result.ok(responseMap);
    }

}
